#!/bin/bash
#     Script which should help users install / tweak some stuff immediately 
#     after installing Ubuntu 12.04 32bit and 64bit.
#
#                           version 0.0.1
#
#     The script comes with no warranty. The author is not responsible if
#     your system breaks.
#
##   
##    Copyright (C) 2012 
##    Valdur Kana <valdur55@gmail.com
##    Arti Zirk   <arti.zirk@gmail.com>
##
##    This program is free software: you can redistribute it and/or modify
##    it under the terms of the GNU General Public License as published by
##    the Free Software Foundation, either version 3 of the License, or
##    (at your option) any later version.
##
##    This program is distributed in the hope that it will be useful,
##    but WITHOUT ANY WARRANTY; without even the implied warranty of
##    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##    GNU General Public License for more details.
##
##    You should have received a copy of the GNU General Public License
##    along with this program. If not, see <http://www.gnu.org/licenses/>.

PROGRAM="Ubuntu-utils" 
VERSION="0.0.1-dev"
AUTOUPDATE="ON"
SCRIPTDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PLUGINS="$SCRIPTDIR/plugins/*.sh"

function SET_C_FILE()
{
for plug in $PLUGINS; do source "$plug"; done
}
################################
## Define message levels
################################

# Set Colors

BOLD="\033[1;10m"
RED="\033[0;31m"
REDBOLD="\033[1;31m"
GREEN="\033[0;32m"
GREENBOLD="\033[1;32m"
YELLOW="\033[0;33m"
YELLOWBOLD="\033[1;33m"
BLUE="\033[0;34m"
BLUEBOLD="\033[1;34m"
ENDCOLOR="\033[0m"

function ERR() #Error case
{
  echo -e $RED "$@" $ENDCOLOR
}

function WARN() #Warning
{
    echo -e $RED "$@" $ENDCOLOR
}

function INFO() #Informational messages, "all ok!"
{
  echo -e $GREEN  "$@" $ENDCOLOR
}

function STATUS()
{
  echo -e $BLUE "$@" $ENDCOLOR
}

##################
## Exit functions
##################

function End()
{
	INFO "The End"
	exit 0
}

function Done()
{
    #echo -e "Done!"
    INFO "Done!"
}

function Terminate()
{
    ERROR "Terminator"
    exit 0
}

function ShowLicense()
{
echo -e $BOLD"$PROGRAM $VERSION Copyright (C) 2011 IT-vabakad"$ENDCOLOR
echo -e ""

echo -e "This program comes with ABSOLUTELY NO WARRANTY. This is free software,\
and you are welcome to redistribute it under the terms \
of GNU GPL V3 or Later."

echo -e ""

echo -e "You should have received a copy of the GNU General Public License \
along with this program. If not, see <http://www.gnu.org/licenses/>"

echo -e ""
}

##################################################
## Update checks downloads RSS feed and downloads
## lastest version to program homedir
## And resets changes file var
##################################################
function Update()
{
STATUS "Checking Update"
#Download downloads RSS
FEED_URL=http://code.google.com/feeds/p/ubuntu-utils/downloads/basic/
#wget -O $HOMEDIR/update.tmp $FEED_URL
#Stdout RSS file, filter lines with </id> and changes file,
#split < and / , print 10. item
latest=$(wget -q -O - $FEED_URL | grep -m1 "$changes_file.*</id>$" | \
        awk -F'<|/' '{print $10}')
DOWNLOAD_URL=http://ubuntu-utils.googlecode.com/files/$latest
#rm -f $HOMEDIR/update.tmp
if [ "$latest" = "$C_FILE" ]; then
	INFO "$PROGRAM is Up-to-date"
	Main
elif [[ "$latest" < "$C_FILE" ]]; then
	WARN "Unknow version"
else
	if [ "$AUTOUPDATE" = "ON" ]; then
		STATUS "Update found. Downloading Update..."
		wget -O "$HOMEDIR/$latest" $DOWNLOAD_URL 
		if [ -e "$HOMEDIR/$latest" ]; then
		chown "$USER" "$HOMEDIR/$latest"
		chmod a+x "$HOMEDIR/$latest"
		INFO "$changes_file Updated!" 
		#--text="An updated version of $PROGRAM has been automatically \
                #downloaded and saved to $HOMEDIR/$latest. Please use that \
                #script instead."
		Done
		SET_C_FILE
		Main
		else
		ERR "Update failed!"
		fi
	else
		WARN "An update to $PROGRAM is available. It might contain \
                    new features and bug fixes. \
                    You are recommended to use the new version instead. \
                    Visit http://code.google.com/p/ubuntu-utils \
                    for changelog and details."$ENDCOLOR
	fi
fi
if [ ! "$latest" = "$C_FILE" ]; then
USINGLATEST="NO"
fi
}

########################################
##Chooser window model
##It checks function file and makes GUI
########################################

function Chooser()
{
    # Functions without descriptions
    #funkts=$(awk "/^function $@/" $HOMEDIR/$C_FILE | \
    #         awk -F'^function|#' '{print $2}')

    # Functions with descriptions
    #funkts_desc=$(awk "/^function $@/" $HOMEDIR/$C_FILE | \
    #              awk -F'^function|#' '{print $3}')
    # When function have description then show it in GUI
    # When functions don't have description then show it's name
    # and makes all functions to selected
    changes=`
    IFS=$'\n'
#     NAME_TRUE=( $(awk -F'#|^function ' '/^function '$1'.*T_/\
#     {print"TRUE";name=$2;print name;getline;\
#     if($2==""){print name}if($2!=""){print$2}}' $HOMEDIR/$C_FILE) )

#     NAME_FALSE=( $(awk -F'#|^function ' '/^function '$1'.*F_/\
#     {print"FALSE";name=$2;print name;getline;\
#     if($2==""){print name}if($2!=""){print$2}}' $HOMEDIR/$C_FILE) )
    NAME=( $(awk -F'#|^function ' '/^function '$1'/{name=$2;
    {if(!match(name, "T_")){print"FALSE"} else {print "TRUE"}};
    print name;desc=$3;
    {if(desc==""){print name} else {print desc}}};' $PLUGINS) )

    unset IFS

    zenity --width=600 --height=500 \
    --title="$main - $PROGRAM $VERSION"\
    --text="Here is one part of changes.... \n$2\nUsing perfix $1" \
    --list --checklist --column="Check" --column="Name" --column="Description" \
    "${NAME[@]}"` #--hide-column=2 --hide-header

    # When user clicks OK
    if [ $? = 0 ]; then
    echo "Changes: $changes"

    # Maybe it isn't good way to run functions this way
    fun=$(echo $changes | tr "|" "\n")
    for x in $fun 
    do
    	STATUS "\n Executing $x \n"
    	${x}
    done

    #And now we execute root commands
    #if [ "$UseRoot" != "" ]; then
    #	fun=$(echo $UseRoot | tr ":" "\n")
    #	for x in $fun
    #	do
    #		STATUS "\n Executing $x as root\n"
    #		echo $x
    #	        # It works!
    #		gksudo "/bin/bash -c 'source $SCRIPTDIR/funktsioonid; $x'"
    #	done
    #	UseRoot=""
    #fi

    fi
Done
Main

}
####################################           
##Main window
##Gives argument to chooser window
####################################
function Main()
{
main=$(zenity --list --width=400 --height=400 --title="$PROGRAM $VERSION" \
--text="Thanks for using $PROGRAM. \
        \n Please make you choise. \
        \n Using file: $C_FILE" \
--radiolist  --column "Choose" --column "Options" \
TRUE "Install programs" \
FALSE "Add repos" \
FALSE "Misc utils" \
FALSE "Conficure programs" \
FALSE "All" \
FALSE "Update" \
FALSE "Close")
if [ $? == 1 ]; then
	End
fi

arr=$(echo $main | tr "\:" "\n")
for x in $arr
do
        case $x in
        "Install") Chooser "I_" "Additional programs";;
        "repos") Chooser "R_" "Add repo";;
        "Misc") Chooser "M_" "Utils";;
        "Confirue") Chooser "C_" "Configure";;
        "Update") Update;;
        "All") Chooser "" "Displaying  all functions";;
        "Close") End
        esac
done
}

#Ready for start

INFO "Start"
ShowLicense
SET_C_FILE
Update
Main
